package org.example.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.pojo.PageResult;
import org.example.pojo.Result;
import org.example.pojo.Student;
import org.example.pojo.StudentQueryParam;
import org.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result page(StudentQueryParam studentQueryParam){
        log.info("分页查询: {}", studentQueryParam);
        PageResult<Student> pageResult = studentService.page(studentQueryParam);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result save(@RequestBody Student student){
        log.info("新增学生: {}", student);
        studentService.save(student);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("修改班级信息:{}",student);
        studentService.update(student);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据id查询员工信息{}", id);
        Student student = studentService.getInfo(id);
        return Result.success(student);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("根据id删除: {}", id);
        studentService.delete(id);
        return Result.success();
    }

    @PutMapping("/violation/{id}/{score}")
    public Result updateViolationInfo(@PathVariable int id, @PathVariable int score) {
        studentService.updateViolationInfo(id, score);
        return Result.success();
    }
}
